package com.cqz.action;

import com.cqz.dao.BookInfo;
import com.cqz.pojo.OrderAndBook;
import com.cqz.dao.OrderInfo;
import com.cqz.services.BookService;
import com.cqz.services.OrderService;
import com.cqz.services.TagService;
import com.cqz.tools.Tools;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class SearchAction {

    @Autowired
    private BookService bookService;
    @Autowired
    private TagService tagService;

    @Autowired
    private OrderService orderService;

    private int initLimit = 14;

    /**
     * 查询书籍并返回信息进行修改
     * @param httpSession
     * @param searchContent
     * @return
     */
    @RequestMapping("searchBookeditAction")
    public String searchBookeditAction(HttpSession httpSession,String searchContent){
        if(Tools.isNull(searchContent)){
            searchContent=(String)httpSession.getAttribute("searchContent");

        }
        if(Tools.isNull(searchContent)){
            httpSession.setAttribute("searchSize",0);
            httpSession.removeAttribute("bookInfoList");
            return "editBook";
        }else {
            httpSession.setAttribute("searchContent",searchContent);
            List<BookInfo> bookInfoList=bookService.searchBookAndEdit(searchContent);
            System.out.println(bookInfoList);
            httpSession.setAttribute("searchSize",bookInfoList.size());
            httpSession.setAttribute("bookInfoList",bookInfoList);
            return "editBook";
        }
    }

    /**
     * 根据书名查询书籍
     * @param request
     * @param model
     * @param httpSession
     * @param searchContent
     * @param user_name
     * @param after_id
     * @param limit
     * @return
     */
    @RequestMapping("searchAction")
    public String searchAction(HttpServletRequest request, Model model, HttpSession httpSession, String searchContent,
                               @SessionAttribute(value = "user_name") String user_name,
                               @RequestParam(value = "after_id",defaultValue = "0") String after_id,
                               @RequestParam(value = "limit",defaultValue = "14") String limit){
        if(Tools.isNull(searchContent)){
            httpSession.setAttribute("searchSize",0);
        }else {
            List<BookInfo> bookInfoList=bookService.searchBookAndEdit(searchContent);
            System.out.println(bookInfoList);
            httpSession.setAttribute("searchSize",bookInfoList.size());
            httpSession.setAttribute("bookInfoList",bookInfoList);
        }
        return "showSearchResult";
    }

    /**
     * 根据标签查询书籍
     * @param httpSession
     * @param searchContent
     * @return
     */
    @RequestMapping("searchByTagAction")
    public String searchByTagAction(HttpSession httpSession, String searchContent){
        if(Tools.isNull(searchContent)){
            httpSession.setAttribute("searchSize",0);
        }else {
            List<BookInfo> bookInfoList=tagService.searchBookAndEdit(searchContent);
            System.out.println(bookInfoList);
            httpSession.setAttribute("searchSize",bookInfoList.size());
            httpSession.setAttribute("bookInfoList",bookInfoList);
        }
        return "showSearchResult";
    }

    /**
     * 显示书籍详情(以及标签)
     * @param model
     * @param book_id
     * @return
     */
    @RequestMapping("/searchBookDetailAction")
    public String searchBookDetailAction(Model model,String book_id){
        if(Tools.isNull(book_id)){
            return "fail";
        }
        else {
            int bid=Integer.parseInt(book_id);
            model.addAttribute("doeBookEntity",bookService.getBookById(bid));
            model.addAttribute("bookRefTags",tagService.getRefTagsByBookID(bid));
            return "bookDetail";
        }
    }

    /**
     * 查询订单信息
     * @param model
     * @param httpSession
     * @return
     */
    @RequestMapping("searchPurchaseRecAction")
    public String searchPurchaseRecAction(Model model,HttpSession httpSession){
        int uid=(Integer) httpSession.getAttribute("uid");
        List<OrderInfo> list=orderService.findByUid(uid);
        List<OrderAndBook> res=new ArrayList<>();
        Iterator<OrderInfo> infoIterator=list.iterator();
        while (infoIterator.hasNext()){
            OrderInfo orderInfo=infoIterator.next();
            OrderAndBook o=new OrderAndBook();
            o.setOdate(orderInfo.getOdate());
            o.setBid(orderInfo.getBid());
            o.setOnum(orderInfo.getOnum());
            o.setOstate(orderInfo.getOstate());
            BookInfo bookInfo=bookService.getBookById(orderInfo.getBid());
            o.setUrl(bookInfo.getBurl());
            o.setBname(bookInfo.getBname());
            res.add(o);
        }
        model.addAttribute("purchaseRecordList",res);
        return "bookList";
    }

    /**
     * 查询购物车信息
     * @param model
     * @param httpSession
     * @return
     */
    @RequestMapping("searchShopTrolleyAction")
    public String searchShopTrolleyAction(Model model,HttpSession httpSession){
        int uid=(Integer) httpSession.getAttribute("uid");
        List<OrderInfo> list=orderService.findByUidNotPay(uid);
        List<OrderAndBook> res=new ArrayList<>();
        Iterator<OrderInfo> infoIterator=list.iterator();
        while (infoIterator.hasNext()){
            OrderInfo orderInfo=infoIterator.next();
            OrderAndBook o=new OrderAndBook();
            o.setOdate(orderInfo.getOdate());
            o.setBid(orderInfo.getBid());
            o.setOnum(orderInfo.getOnum());
            o.setOstate(orderInfo.getOstate());
            BookInfo bookInfo=bookService.getBookById(orderInfo.getBid());
            o.setUrl(bookInfo.getBurl());
            o.setBname(bookInfo.getBname());
            o.setOprice(bookInfo.getBprice());
            res.add(o);
        }
        model.addAttribute("shoppingList",res);
        return "shoppingTrolley";
    }

    /**
     * 管理员分页查询订单
     * @param page
     * @param model
     * @param uid
     * @param bid
     * @param ostate
     * @param sdate
     * @param edate
     * @return
     */
    @RequestMapping(value = "searchOrderAction" )
    public String searchOrderAction(@RequestParam(required = true,defaultValue = "1")int page, Model model, String uid,String bid,
                                    String ostate,String sdate,String edate){
        model.addAttribute("userid",uid);model.addAttribute("bid",bid);model.addAttribute("ostate",ostate);
        model.addAttribute("sdate",sdate);model.addAttribute("edate",edate);
        PageHelper.startPage(page,5);
        Date starDate=Tools.getDate(sdate);
        Date endDate=Tools.getDate(edate);
        if(Tools.compareDate(starDate,endDate)){

        }else {
            model.addAttribute("error","开始日期必须小于等于结束日期");
        }
        HashMap<String,Object> map=new HashMap<>();
        if(Tools.isNull(uid)){
            uid=null;
        }
        if(Tools.isNull(bid)){
            bid=null;
        }
        map.put("uid",uid);map.put("bid",bid);map.put("ostate",ostate);
        map.put("sdate",starDate);map.put("edate",endDate);
        List<OrderAndBook> res;
        if(ostate.equals("未付款")){
            res=orderService.selectAllOrders(map);
        }else {
            res=orderService.selectKindOrders(map);
        }
        PageInfo<OrderAndBook> p=new PageInfo<>(res);
        model.addAttribute("page",p);

        model.addAttribute("list",res);
        return "showSearchOrderResult";
    }

    /**
     * 管理员根据订单id查询一个订单
     * @param page
     * @param userid
     * @param bid
     * @param ostate
     * @param sdate
     * @param edate
     * @param oid
     * @param model
     * @return
     */
    @RequestMapping("searchOneOrderAction")
    public String searchOneOrderAction(String page,String userid,String bid,String ostate,
                                       String sdate,String edate,String oid,Model model){
        model.addAttribute("userid",userid);model.addAttribute("bid",bid);model.addAttribute("ostate",ostate);
        model.addAttribute("sdate",sdate);model.addAttribute("edate",edate);model.addAttribute("page",page);
        OrderInfo orderInfo=orderService.findOrderByOid(Integer.parseInt(oid));
        model.addAttribute("uid",orderInfo.getUid());
        OrderAndBook orderAndBook=new OrderAndBook();
        orderAndBook.setOid(orderInfo.getOid());
        BookInfo bookInfo=bookService.getBookById(orderInfo.getBid());
        orderAndBook.setUrl(bookInfo.getBurl());
        orderAndBook.setBname(bookInfo.getBname());
        orderAndBook.setOnum(orderInfo.getOnum());
        orderAndBook.setOstate(orderInfo.getOstate());
        orderAndBook.setOamount(orderInfo.getOamount());
        orderAndBook.setOdate(orderInfo.getOdate());
        orderAndBook.setBid(orderInfo.getBid());
        orderAndBook.setUid(orderInfo.getUid());
        model.addAttribute("order",orderAndBook);
        model.addAttribute("date",Tools.date2Str(orderAndBook.getOdate()));
        return "editOrder";
    }
}
